package com.fire.web.modules.all.controller;

import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.core.util.DateUtil;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.*;
import com.fire.web.modules.all.service.*;
import com.fire.web.modules.all.utils.TokenUtil;
import com.fire.web.modules.all.validation.StatisticsDTO;
import com.fire.web.modules.sso.service.IOAuthService;
import com.fire.web.modules.sys.entity.Role;
import com.fire.web.modules.sys.entity.UserRole;
import com.fire.web.modules.sys.service.IRoleService;
import com.fire.web.modules.sys.service.IUserRoleService;
import com.fire.web.security.shiro.realm.UserRealm;
import com.smartthing.spi.SmartThingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: StatisticsController
 * @Description: TODO
 * @Author raoyuxing
 * @Date 2019/2/23 17:06
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AssetController.class);

    @Autowired
    private IStatisticsService statisticsService;

    @Autowired
    private IOAuthService oAuthService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleService roleService;


    /**
    * 功能描述: 站点维度的统计结果
    * @param:
    * @return: AjaxResult
    * @auther: raoyuxing
    * @date: 2019/2/23 17:09
    */
    @PostMapping(value = "/statistics")
    @ResponseBody
    public AjaxResult statistics(@Validated StatisticsDTO statisticsDTO, BindingResult bindingResult
                                            ,HttpServletRequest request, HttpServletResponse response) {
        if (bindingResult.hasErrors()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                LOGGER.error(fieldError.getDefaultMessage());
            }
            return new AjaxResult(ErrorCodeConstants.PARAM_WRONGFULL, "参数错误", "参数错误");
        }
        String code = getRoleByToken(request,response);
        Map<String ,Object> map = new HashMap<>();
        try {
            if ("supervisor".equals(code)){
                map = statisticsService.supervisorStatistics(request, response);
            }else if ("proprietor".equals(code)){
                map = statisticsService.proprietorStatistics(request, response);
            }else if ("maintainor".equals(code)){

            }else {

            }
        } catch (SmartThingException e) {
            e.printStackTrace();
            return new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统错误", "系统错误");
        }

        return new AjaxResult(ErrorCodeConstants.SUCCESS, "成功", map, "成功");
    }

    /**
     * 功能描述: 根据token获取siteid
     * @param: request response
     * @return: siteid
     * @auther: raoyuxing
     * @date: 2019/2/22 14:54
     */
    private String getRoleByToken(HttpServletRequest request, HttpServletResponse response){

        String token = TokenUtil.getToken(request, response);
        UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
        String id = principalByAccessToken.getId();

        EntityWrapper<UserRole> userRoleWrapper = new EntityWrapper<>();
        userRoleWrapper.eq("user_id",id);
        String roleId = userRoleService.selectOne(userRoleWrapper).getRoleId();

        EntityWrapper<Role> roleWrapper = new EntityWrapper<>();
        roleWrapper.eq("id",roleId);
        String code = roleService.selectOne(roleWrapper).getCode();

        return code;
    }

}
